<?php

use yii\helpers\Html;
use yii\grid\GridView;
use app\models\BuyerCashback;

/* @var $this yii\web\View */
/* @var $searchModel app\models\search\BuyerCashbackSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '买家返现';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="buyer-cashback-index">
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            [
                'attribute'=>'auth_shop_id',
                'filter'=>false,
                'format'=>'raw',
                'value'=>function($model){
                    return $model->authShop?$model->authShop->name:'';
                }
            ],
            'order_no',
            'content:ntext',
            'user_account',
            'user_realname',
            [
                'label'=>'图片(点击放大)',
                'filter'=>false,
                'format'=>'raw',
                'value'=>function($model){
                    if(empty($model->pics)) return ;
                    $str = '';
                    foreach ($model->pics as $pic){
                        $str.=Html::img($pic->pic->getFileUrl(),['width'=>60,'onclick'=>'showpic(this)']);
                    }
                    return $str;
                }
            ],
            [
                'attribute'=>'return_fee',
                'filter'=>false,
                'format'=>'raw',
                'value'=>function($model){
                    return sprintf('%.2f',$model->return_fee/100).'元';
                }
            ],
            [
                'attribute'=>'ctime',
                'options' => ['width' => '160px'],
                'filter'=>false,
                'format'=>'raw',
                'value'=>function($model){
                    return date('Y-m-d H:i:s',$model->ctime);
                }
            ],
            [
                'attribute'=>'state',
                'options' => ['width' => '80px'],
                'filter'=>\app\models\BuyerCashback::$state_arr,
                'format'=>'raw',
                'value'=>function($model){
                    return $model->showState();
                }
            ],
            [
                'label'=>'操作',
                'options' => ['width' => '80px'],
                'filter'=>BuyerCashback::$state_arr,
                'format'=>'raw',
                'value'=>function($model){
                    if($model->state==BuyerCashback::STATE_CHECKING){
                        $str = Html::button('通过',['class'=>'btn btn-success','onclick'=>'updatestate('.$model->id.','.BuyerCashback::STATE_SUCCESS.')']);
                        $str .= Html::button('失败',['class'=>'btn btn-danger','onclick'=>'updatestate('.$model->id.','.BuyerCashback::STATE_FAIL.')']);

                    }else{
                        $str = Html::button('重新审核',['class'=>'btn btn-primary','onclick'=>'updatestate('.$model->id.','.BuyerCashback::STATE_CHECKING.')']);

                    }
                    return $str;
                }
            ],

//            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>


</div>
<div class="modal fade text-center" id="showImgModal">
    <div class="modal-dialog modal-lg" style="display: inline-block; width: auto;">
        <div class="modal-content">
            <img id="showImage" src="">
        </div>
    </div>
</div>
<script>
    function showpic(obj) {
        console.log(obj.src);
        $('#showImage').attr('src',obj.src);
        $('#showImgModal').modal();
    }
    function updatestate(cashback_id,state) {
        $.post('/agent/ajax/cashback-update',{cashback_id:cashback_id,state:state},function (res) {
            console.log(res);
            res = checkresult(res);
            if(res){
                alert('更改完成');
                window.location.reload();
            }
        });

    }
</script>